import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/business.dart';
import 'package:flutter_app/custom_banner.dart';
import 'package:flutter_app/mine.dart';

void main() {
  runApp(MyApp());
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  int _currentIndex = 0;

  List<Widget> _pageList = [
    HomePage(),
    Center(
      child: Business(),
    ),
    Center(
      child: Mine(),
    ),
  ];

  List<BottomNavigationBarItem> _barItem = [
    BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
    BottomNavigationBarItem(icon: Icon(Icons.list), label: '业务'),
    BottomNavigationBarItem(icon: Icon(Icons.people), label: '我的'),
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Scaffold(
        body: IndexedStack(
          index: _currentIndex,
          children: this._pageList,
        ),
        bottomNavigationBar: BottomNavigationBar(
          onTap: (int index) {
            setState(() {
              this._currentIndex = index;
            });
          },
          currentIndex: this._currentIndex,
          items: _barItem,
          selectedItemColor: Color(0XFFf36f32),
          unselectedItemColor: Color(0XFF999999),
          selectedFontSize: 12,
          type: BottomNavigationBarType.fixed,
        ),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  HomePageState createState() => HomePageState();
}

class HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {
  late ScrollController _controller;
  double progress = 0.2;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    _controller = ScrollController();
    _controller.addListener(() {
      int offset = _controller.position.pixels.toInt();
      print("滑动距离$offset");
      progress = offset / _controller.position.maxScrollExtent + 0.2;
      print("===========" + progress.toString());
      setState(() {});
    });
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
//      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text(
          "您好，中国平安数控有限公司",
          style: TextStyle(
            color: Color(0xFF333333),
            fontWeight: FontWeight.bold,
            fontSize: 20,
          ),
        ),
        actions: <Widget>[
          Container(
            width: 60,
            height: double.infinity,
            child: Stack(
              children: <Widget>[
                Positioned(
                  top: 23,
                  right: 13,
                  child: Image.asset(
                    "assets/image/icon_msg.png",
                    width: 20,
                    height: 20,
                  ),
                ),
                Positioned(
                  top: 13,
                  right: 8,
                  child: Container(
                    width: 16,
                    height: 16,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: Colors.red,
                      borderRadius: BorderRadius.all(Radius.circular(10)),
                      border: Border.all(width: 0, style: BorderStyle.none),
                    ),
                    child: Text(
                      "1",
                      style: TextStyle(
                        fontSize: 10,
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
        ],
        systemOverlayStyle: SystemUiOverlayStyle(
          statusBarBrightness: Brightness.dark, // or Brightness.light
        ),
        backgroundColor: Colors.white,
        elevation: 0,
      ),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(
                top: 15,
                left: 18,
                bottom: 15,
              ),
              child: SizedBox(
                height: 34,
                width: 100,
                child: TextButton.icon(
                  onPressed: () {
                    print("Button pressed");
                  },
                  icon: Image.asset(
                    "assets/image/icon_switch.png",
                    height: 12,
                  ),
                  label: Text(
                    "切换公司",
                    style: TextStyle(
                      color: Color(0xFF333333),
                      fontSize: 12,
                    ),
                  ),
                  style: TextButton.styleFrom(
                    backgroundColor: Colors.white, // replaces `color`
                  ),
                ),
                // FlatButton.icon(
                //   onPressed: () {},
                //   shape: RoundedRectangleBorder(
                //     borderRadius: BorderRadius.all(Radius.circular(30)),
                //     side: BorderSide(
                //       color: Color(0xFFD2D2D2),
                //       style: BorderStyle.solid,
                //       width: 1,
                //     ),
                //   ),
                //   padding: EdgeInsets.all(0.0),
                //   icon: Image.asset(
                //     "assets/image/icon_switch.png",
                //     height: 10,
                //   ),
                //   label: Text(
                //     "切换公司",
                //     style: TextStyle(
                //       color: Color(0xFF333333),
                //       fontSize: 10,
                //     ),
                //   ),
                //   materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                // ),
              ),
            ),
            Container(
              height: 255,
              width: MediaQuery.of(context).size.width,
              child: Stack(
                children: <Widget>[
                  Positioned(
                    top: 90,
                    child: Container(
                      margin: EdgeInsets.symmetric(
                        horizontal: 18,
                      ),
                      width: MediaQuery.of(context).size.width - 36,
                      height: 150,
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          begin: Alignment.topRight,
                          end: Alignment.bottomCenter,
                          colors: [
                            Color(0xFFF8F9FD),
                            Color(0xFFE5E5E5),
                          ],
                        ),
                        borderRadius: BorderRadius.all(Radius.circular(8)),
                      ),
                    ),
                  ),
                  Positioned(
                    top: 80,
                    child: Container(
                      margin: EdgeInsets.symmetric(
                        horizontal: 18,
                      ),
                      width: MediaQuery.of(context).size.width - 36,
                      height: 125,
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: [
                            Color(0xFFE5E5E5),
                            Color(0x66F99349),
                          ],
                        ),
                        borderRadius: BorderRadius.all(Radius.circular(8)),
                      ),
                    ),
                  ),
                  Positioned(
                    child: Container(
                      margin: EdgeInsets.symmetric(
                        horizontal: 18,
                      ),
                      width: double.infinity,
                      height: 210,
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          begin: Alignment.topLeft,
                          end: Alignment.bottomRight,
                          colors: [
                            Color(0xFFF05A23),
                            Color(0xFFF99349),
                          ],
                        ),
                        borderRadius: BorderRadius.all(Radius.circular(8)),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          SizedBox(
                            height: 25,
                          ),
                          Row(
                            children: <Widget>[
                              SizedBox(
                                width: 18,
                              ),
                              Text(
                                "剩余可用额度(元)",
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 13,
                                ),
                              ),
                              Expanded(
                                child: Container(),
                              ),
                              Container(
                                height: 26,
                                width: 84,
                                decoration: BoxDecoration(
                                  gradient: LinearGradient(
                                    begin: Alignment.topCenter,
                                    end: Alignment.bottomCenter,
                                    colors: [
                                      Color(0xFFFFEF52),
                                      Color(0xFFFFAE45),
                                    ],
                                  ),
                                  borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(50),
                                    bottomLeft: Radius.circular(50),
                                  ),
                                ),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Text(
                                      "融资详情",
                                      style: TextStyle(
                                        color: Color(0xFF333333),
                                        fontSize: 13,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    ),
                                    Icon(
                                      Icons.arrow_forward_ios,
                                      size: 11,
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                          SizedBox(
                            height: 5,
                          ),
                          Padding(
                            padding: EdgeInsets.only(
                              left: 18,
                            ),
                            child: Text(
                              "530,900.00",
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 40,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(
                              left: 18,
                            ),
                            child: Text(
                              "授信有效期 2025年2月30日",
                              style: TextStyle(
                                color: Color(0X88FFFFFF),
                                fontSize: 13,
                              ),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(
                              left: 18,
                              top: 23,
                              right: 18,
                            ),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  child: Text(
                                    "授信额度(元) \n600,000.00",
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 13,
                                    ),
                                  ),
                                ),
                                Container(
                                  color: Colors.white,
                                  width: 1,
                                  height: 30,
                                ),
                                Expanded(
                                  child: Padding(
                                    padding: EdgeInsets.only(left: 30),
                                    child: Text(
                                      "已用额度(元) \n69,100.00",
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 13,
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Positioned(
                    child: Container(
                      width: double.infinity,
                      alignment: Alignment.bottomCenter,
                      margin: EdgeInsets.only(
                        bottom: 15,
                      ),
                      child: SizedBox(
                        height: 35,
                        child: TextButton.icon(
                          onPressed: () {
                            print("Button pressed");
                          },
                          icon: Image.asset(
                            "assets/image/icon_finance.png",
                            height: 14,
                            width: 14,
                          ),
                          label: Text(
                            "一键融资",
                            style: TextStyle(
                              color: Color(0xFF333333),
                              fontSize: 14,
                            ),
                          ),
                        ),
                        // FlatButton.icon(
                        //   onPressed: () {},
                        //   padding: EdgeInsets.all(0.0),
                        //   icon: Image.asset(
                        //     "assets/image/icon_finance.png",
                        //     height: 20,
                        //     width: 20,
                        //   ),
                        //   label: Text(
                        //     "一键融资",
                        //     style: TextStyle(
                        //       color: Color(0xFF333333),
                        //       fontSize: 18,
                        //     ),
                        //   ),
                        //   materialTapTargetSize:
                        //       MaterialTapTargetSize.shrinkWrap,
                        // ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 30,
            ),
            Container(
              height: 90,
              margin: EdgeInsets.symmetric(horizontal: 30),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  getCenterItem("申请记录", "assets/image/icon_apply-record.png"),
                  getCenterItem("票据信息", "assets/image/icon_bill-info.png"),
                  getCenterItem("发票验真", "assets/image/icon_invoice-check.png"),
                  getCenterItem("联系客服", "assets/image/icon_contact.png"),
                ],
              ),
            ),
            CustomBanner(
              height: 90,
              autoPlay: true,
              viewportFraction: 0.9,
              items: <Widget>[
                getBannerItem(0),
                getBannerItem(1),
                getBannerItem(2),
              ],
            ),
            SizedBox(
              height: 30,
            ),
            Row(
              children: <Widget>[
                SizedBox(
                  width: 30,
                ),
                Text.rich(
                  TextSpan(
                    children: [
                      TextSpan(
                        text: "租赁",
                        style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.bold,
                          color: Color(0XFFF05A23),
                        ),
                      ),
                      TextSpan(
                        text: "商城",
                        style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Container(),
                ),
                Text(
                  "逛逛商城 ",
                  style: TextStyle(
                    color: Color(0xFF333333),
                    fontSize: 15,
                  ),
                ),
                Icon(
                  Icons.arrow_forward_ios,
                  size: 15,
                  color: Color(0xFFCCCCCC),
                ),
                SizedBox(
                  width: 30,
                ),
              ],
            ),
            SizedBox(
              height: 10,
            ),
            Container(
              height: 170,
              width: MediaQuery.of(context).size.width,
              child: ListView(
                scrollDirection: Axis.horizontal,
                controller: _controller,
                children: <Widget>[
                  SizedBox(
                    width: 20,
                  ),
                  getCenterHItem(
                      "assets/image/image_1.png", "美的家用高端微鲸系列微波炉...", "7800金豆"),
                  getCenterHItem(
                      "assets/image/image_1.png", "炫彩九阳多功能保温杯", "2900金豆"),
                  getCenterHItem(
                      "assets/image/image_1.png", "美的一人份小食锅", "5888金豆"),
                  getCenterHItem(
                      "assets/image/image_1.png", "美的家用高端微鲸系列微波炉...", "7800金豆"),
                  getCenterHItem(
                      "assets/image/image_1.png", "炫彩九阳多功能保温杯", "2900金豆"),
                  getCenterHItem(
                      "assets/image/image_1.png", "美的一人份小食锅", "5888金豆"),
                ],
              ),
            ),
            SizedBox(
              height: 15,
            ),
            Center(
              child: Container(
                width: 40,
                height: 3,
                alignment: Alignment.center,
                child: LinearProgressIndicator(
                  value: progress,
                  valueColor:
                      new AlwaysStoppedAnimation<Color>(Color(0xFFF05A23)),
                  backgroundColor: Color(0xffE6E6E6),
                ),
              ),
            ),
            SizedBox(
              height: 30,
            ),
            Card(
              margin: EdgeInsets.symmetric(horizontal: 20),
              elevation: 3,
              child: Container(
                width: double.infinity,
                padding: EdgeInsets.all(18),
                child: Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Text.rich(
                          TextSpan(
                            children: [
                              TextSpan(
                                text: "租赁",
                                style: TextStyle(
                                  fontSize: 20,
                                  fontWeight: FontWeight.bold,
                                  color: Color(0XFFF05A23),
                                ),
                              ),
                              TextSpan(
                                text: "精选",
                                style: TextStyle(
                                  fontSize: 20,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ],
                          ),
                        ),
                        Expanded(
                          child: Container(),
                        ),
                        Text(
                          "查看更多 ",
                          style: TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 15,
                          ),
                        ),
                        Icon(
                          Icons.arrow_forward_ios,
                          size: 15,
                          color: Color(0xFFCCCCCC),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: 30,
                    ),
                    Row(
                      children: <Widget>[
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                "更快更便捷，APP可以领电子发票啦！",
                                style: TextStyle(
                                  color: Color(0xFF333333),
                                  fontWeight: FontWeight.bold,
                                  fontSize: 16,
                                ),
                              ),
                              SizedBox(
                                height: 10,
                              ),
                              Text(
                                "说起开发皮那些事儿，从事财务的朋友们可一点都不陌生…",
                                style: TextStyle(
                                  color: Color(0xFF333333),
                                  fontSize: 12,
                                ),
                              ),
                            ],
                          ),
                        ),
                        Image.asset("assets/image/image_2.png")
                      ],
                    ),
                  ],
                ),
              ),
            ),
            SizedBox(
              height: 30,
            ),
          ],
        ),
      ),
    );
  }

  Widget getBannerItem(int index) {
    return Stack(
      alignment: Alignment.center,
      children: <Widget>[
        Container(
          padding: EdgeInsets.symmetric(
            horizontal: 5,
          ),
          child: Image.asset(
            "assets/image/image_3.png",
            width: double.infinity,
            fit: BoxFit.fitWidth,
          ),
        ),
        Positioned(
          bottom: 6,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [0, 1, 2].map((s) {
              return Padding(
                padding: const EdgeInsets.symmetric(horizontal: 3.0),
                child: s == [0, 1, 2][index % 3]
                    ? Container(
                        width: 9,
                        height: 5,
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.all(
                            Radius.circular(10),
                          ),
                        ),
                      )
                    : ClipOval(
                        child: Container(
                          width: 5,
                          height: 5,
                          color: Colors.grey,
                        ),
                      ),
              );
            }).toList(),
          ),
        )
      ],
    );
  }

  Widget getCenterItem(String title, String iconData) {
    return Column(
      children: <Widget>[
        Image.asset(
          iconData,
          width: 30,
          height: 30,
        ),
        SizedBox(
          height: 15,
        ),
        Text(
          title,
          style: TextStyle(
            fontSize: 14,
          ),
        ),
      ],
    );
  }

  Widget getCenterHItem(String url, String des, String price) {
    return Container(
      width: 112,
      height: 170,
      alignment: Alignment.center,
      padding: EdgeInsets.all(10),
      margin: EdgeInsets.all(6),
      decoration: BoxDecoration(
        border: Border.all(width: 1, color: Colors.grey[200] as Color),
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Expanded(
            child: Image.asset(url),
          ),
          Text(
            des,
            style: TextStyle(
              fontSize: 12,
              color: Color(0xFF333333),
            ),
          ),
          SizedBox(
            height: 8,
          ),
          Text(
            price,
            style: TextStyle(
              fontSize: 13,
              color: Color(0xFFF05A23),
            ),
          ),
        ],
      ),
    );
  }
}
